package com.wl156.activities.find;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.activities.Xinyongxingji;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.model.Wl156DB;
import com.wl156.utils.Utils;

public class GoodInfo extends BaseActivity implements OnClickListener {

	private static final String TAG = null;
	private TextView mProvice;
	private TextView mCity;
	private TextView mRegion;
	private TextView mToProvice;
	private TextView mToCity;
	private TextView mToRegion;
	private TextView mName;
	private TextView mWeight;
	private TextView mLength;
	private TextView mContact;
	private TextView mPhone;
	private TextView mNote;
	private TextView mNeedCarModelText;
	private TextView mNeddCarCount;
	private TextView mSetOffDate;
	private TextView mXinyongScore;
	private TextView mWeightUnit;
	private Button mXinyongDetail;

	private ImageButton mCall;
	private Button mCollect;
	private Button mChengjiao;

	private long mGoodId;
	private String userID;

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	boolean mOrderSelectAction;
	Button mOrderSelectButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_good_info);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mGoodId = getIntent().getLongExtra("id", 0);

		mName = (TextView) findViewById(R.id.name);
		mWeight = (TextView) findViewById(R.id.weight);
		mLength = (TextView) findViewById(R.id.length);
		mContact = (TextView) findViewById(R.id.contact);
		mPhone = (TextView) findViewById(R.id.phone);
		mNote = (TextView) findViewById(R.id.note);
		mProvice = (TextView) findViewById(R.id.provice);
		mToProvice = (TextView) findViewById(R.id.to_provice);
		mNeedCarModelText = (TextView) findViewById(R.id.need_car_model_text);
		mNeddCarCount = (TextView) findViewById(R.id.need_car_count);
		mXinyongScore = (TextView) findViewById(R.id.xinyongscore);
		mXinyongDetail = (Button) findViewById(R.id.xinyongdetail);
		mXinyongDetail.setOnClickListener(this);

		mWeightUnit = (TextView) findViewById(R.id.weight_unit);

		mSetOffDate = (TextView) findViewById(R.id.setOffDate);

		mCall = (ImageButton) findViewById(R.id.call);
		mCall.setOnClickListener(this);
		mCollect = (Button) findViewById(R.id.collect);
		mCollect.setOnClickListener(this);
		mChengjiao = (Button) findViewById(R.id.chengjiao);
		mChengjiao.setOnClickListener(this);
//		mChengjiao.setVisibility(View.VISIBLE);

		mOrderSelectAction = getIntent().getBooleanExtra("mOrderSelectAction",
				false);
		Log.d(TAG, "mOrderSelectAction: " + mOrderSelectAction);
		mOrderSelectButton = (Button) findViewById(R.id.select_driver);
//		mOrderSelectButton.setOnClickListener(this);
		// if (mOrderSelectAction) {
		// mOrderSelectButton.setVisibility(View.VISIBLE);
		// } else {
		// mOrderSelectButton.setVisibility(View.GONE);
		// }

		new AsyncTask<Void, Void, Object>() {
			ProgressDialog mDialog;

			@Override
			protected void onPostExecute(Object result) {

				// 10-24 11:35:56.820: D/(31237): result: anyType{string=45054;
				// string=广东珠海香洲; string=北京北京; string=广州文昌物流有限公司; string=粽子;
				// string=1; string=货车; string=10; string=梁先生;
				// string=18666567533; string=短号; string=06月29日; string=null;
				// string=1; string=四星级; string=2; string=1; string=1;
				// string=四星级; string=-1; string=吨; }, type: SoapObject

				try {
					if (result instanceof SoapObject) {
						SoapObject so = (SoapObject) result;
						String id = Utils.propertyToShow(so.getProperty(0));
						String start = Utils.propertyToShow(so.getProperty(1));
						String end = Utils.propertyToShow(so.getProperty(2));
						String companyName = Utils.propertyToShow(so
								.getProperty(3));
						String goodName = Utils.propertyToShow(so
								.getProperty(4));
						String weight = Utils.propertyToShow(so.getProperty(5));
						String needType = Utils.propertyToShow(so
								.getProperty(6));
						String needLength = Utils.propertyToShow(so
								.getProperty(7));
						String contact = Utils
								.propertyToShow(so.getProperty(8));
						String phone = Utils.propertyToShow(so.getProperty(9));
						String internalPhone = Utils.propertyToShow(so
								.getProperty(10));
						String setdateoff = Utils.propertyToShow(so
								.getProperty(11));
						String note = Utils.propertyToShow(so.getProperty(12));
						String needCarCount = Utils.propertyToShow(so
								.getProperty(13));
						String creditScore = Utils.propertyToShow(so
								.getProperty(14));
						String finishOrderCount = Utils.propertyToShow(so
								.getProperty(15));
						mName.setText(goodName);
						mProvice.setText(start);
						mToProvice.setText(end);
						mWeight.setText(weight);
						mNeedCarModelText.setText(needType);
						mNeddCarCount.setText(needCarCount);
						mLength.setText(needLength);
						mContact.setText(contact);
						mPhone.setText(phone);
						mNote.setText(note);
						mSetOffDate.setText(setdateoff);
						mXinyongScore.setText(creditScore);
						mWeightUnit.setText(Utils.propertyToShow(so
								.getProperty(20)));

						userID = Utils.propertyToShow(so.getProperty(17));
					} else {
						Toast.makeText(GoodInfo.this, "获取信息失败，请稍候再试",
								Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					Toast.makeText(GoodInfo.this, "信息已被删除", Toast.LENGTH_LONG)
							.show();
					finish();
				}
				mDialog.dismiss();
			};

			@Override
			protected void onPreExecute() {
				mDialog = ProgressDialog.show(GoodInfo.this, "", "加载中");
			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.commonFindFreightMessageDetail(
							mCertificate, mGoodId);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}
		}.execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.chengjiao: {
			Log.d(TAG, "chengjiao");
			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(GoodInfo.this, "", "加载中");
				};

				@Override
				protected void onPostExecute(Object result) {
					mDialog.dismiss();
					Toast.makeText(GoodInfo.this, "成功", Toast.LENGTH_LONG)
							.show();
				}

				@Override
				protected Object doInBackground(Void... params) {
					try {
						Wl156DB.changeCarOrGoodFinish(GoodInfo.this, "2",
								String.valueOf(mGoodId));
						return mWebServiceClient.company_CargosInfo(
								mCertificate, String.valueOf(mGoodId));
					} catch (IOException e) {
						e.printStackTrace();
						return e;
					} catch (XmlPullParserException e) {
						e.printStackTrace();
						return e;
					}
				}
			}.execute();
		}
			break;
		case R.id.xinyongdetail: {
			Intent i = new Intent(this, Xinyongxingji.class);
			i.putExtra("userID", userID);
			startActivity(i);
		}
			break;
		case R.id.order_select: {
			Intent i = new Intent(GoodInfo.this, FindCar.class);
			startActivity(i);
			finish();
		}
			break;
		case R.id.call:
			Intent i = new Intent(Intent.ACTION_DIAL);
			i.setData(Uri.parse("tel:" + mPhone.getText().toString()));
			startActivity(i);
			break;
		case R.id.collect:
			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(GoodInfo.this, "", "加载中");
				};

				@Override
				protected void onPostExecute(Object result) {
					mDialog.dismiss();

					try {
						if (result instanceof SoapObject) {
							SoapObject so = (SoapObject) result;
							final String[] ids = new String[so
									.getPropertyCount()];
							String[] names = new String[so.getPropertyCount()];
							String[] counts = new String[so.getPropertyCount()];
							if (counts.length == 0) {
								Toast.makeText(GoodInfo.this, "没有收藏分组",
										Toast.LENGTH_SHORT).show();
								return;
							}
							for (int i = 0; i < so.getPropertyCount(); i++) {
								SoapObject line = (SoapObject) so
										.getProperty(i);
								if (line.getPropertyCount() == 0) {
									Toast.makeText(GoodInfo.this, "没有收藏分组",
											Toast.LENGTH_SHORT).show();
									return;
								}
								String id = Utils.propertyToShow(line
										.getProperty(0));
								ids[i] = id;
								String name = Utils.propertyToShow(line
										.getProperty(1));
								names[i] = name;
								String count = Utils.propertyToShow(line
										.getProperty(2));
								counts[i] = count;
							}

							AlertDialog.Builder builer = new Builder(
									GoodInfo.this);
							builer.setItems(names,
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												final int which) {
											new AsyncTask<Void, Void, Object>() {
												ProgressDialog mDialog;

												@Override
												protected void onPreExecute() {
													mDialog = ProgressDialog
															.show(GoodInfo.this,
																	"", "添加中");
												};

												@Override
												protected void onPostExecute(
														Object result) {
													if (result instanceof SoapObject) {
														SoapObject so = (SoapObject) result;
														Toast.makeText(
																GoodInfo.this,
																"收藏成功",
																Toast.LENGTH_LONG)
																.show();
													} else if (result instanceof SoapPrimitive) {
														Toast.makeText(
																GoodInfo.this,
																result.toString(),
																Toast.LENGTH_LONG)
																.show();
													} else {
														Toast.makeText(
																GoodInfo.this,
																"网络异常，添加失败，请稍候再试",
																Toast.LENGTH_LONG)
																.show();
													}
													mDialog.dismiss();
												};

												@Override
												protected Object doInBackground(
														Void... params) {
													try {
														return mWebServiceClient
																.driverModifyStoreGroupAddFreightMessageToGroup(
																		mCertificate,
																		String.valueOf(mGoodId),
																		ids[which]);
													} catch (IOException e) {
														return e;
													} catch (XmlPullParserException e) {
														return e;
													}
												}
											}.execute();
										}
									});
							builer.show();
						} else {
							Toast.makeText(GoodInfo.this, "联网失败，请稍候再试",
									Toast.LENGTH_LONG).show();
						}
					} catch (Exception e) {
						Toast.makeText(GoodInfo.this, "系统异常，请稍候再试",
								Toast.LENGTH_LONG).show();
						e.printStackTrace();
					}
					
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient
								.commonGetMyStoreGroups(mCertificate);
					} catch (IOException e) {
						return e;
					} catch (XmlPullParserException e) {
						return e;
					}
				}
			}.execute();
			break;
		}
	}
}
